package cn.itcast.controller;

import cn.itcast.domain.Goods;
import cn.itcast.domain.Msg;
import cn.itcast.domain.Person;
import cn.itcast.service.IGoodsService;
import cn.itcast.service.IPersonService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
public class GoodsController {

    @Autowired
    private IGoodsService goodsService;

    @Autowired
    private IPersonService personService;

    @RequestMapping("/donateGoods")
    public String findAllDonateGoods(Model model,@RequestParam(value="pn",required = false,defaultValue = "1")Integer pn){
        //开启分页查询，获取第pn页，每页5条数据
        PageHelper.startPage(pn,6);
        // 查询所有捐赠物资
        List<Goods> goodsList = goodsService.findAllDonateGoods();
        // 将物资存入model中，传给前端
        model.addAttribute("list",goodsList);
        //获取分页信息
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList,3);
        //分页信息转为JSON传给前端
        model.addAttribute("pageInfo", JSONObject.toJSON(pageInfo));
        // 跳转到DonateGoodsList.jsp页面
        return "donateGoodsList";
    }

    @RequestMapping("/findDonateGoodsById/")
    @ResponseBody
    public Msg findDonateGoodsById(@RequestParam("id") Integer id){
        // 根据id查询捐赠物资
        Goods donateGoods = goodsService.findDonateGoodsById(id);
        // 返回数据
        return Msg.success().add("donateGoods",donateGoods);
    }

    @RequestMapping(value = "/insertDonateGoods", method = RequestMethod.POST)
    public String insertDonateGoods(Goods goods){
        // 调用service层方法
        goodsService.insertDonateGoods(goods);
        // 重定向到donateGoods请求
        return "redirect: donateGoods";
    }

    @RequestMapping(value = "/updateDonateGoods", method = RequestMethod.POST)
    @ResponseBody
    public Msg updateDonateGoods(Goods goods){
        // 调用service层方法
        goodsService.updateDonateGoods(goods);
        // 返回成功信息
        return Msg.success();
    };

    @RequestMapping(value = "/deleteDonateGoods", method = RequestMethod.POST)
    public String deleteDonateGoods(@RequestParam("id")Integer id){
        // 调用service层方法
        goodsService.deleteDonateGoods(id);
        // 重定向到donateGoods请求
        return "redirect: donateGoods";
    }

    @RequestMapping("/recieveGoods")
    public String findAllRecieveGoods(Model model,@RequestParam(value="pn",required = false,defaultValue = "1")Integer pn){
        //开启分页查询，获取第pn页，每页5条数据
        PageHelper.startPage(pn,6);
        // 查询所有接收物资
        List<Goods> goodsList = goodsService.findAllRecieveGoods();
        // 将物资存入model中，传给前端
        model.addAttribute("list",goodsList);
        //获取分页信息
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList,3);
        //分页信息转为JSON传给前端
        model.addAttribute("pageInfo", JSONObject.toJSON(pageInfo));
        // 跳转到recieveGoodsList.jsp页面
        return "recieveGoodsList";
    }

    @RequestMapping("/findRecieveGoodsById/")
    @ResponseBody
    public Msg findRecieveGoodsById(@RequestParam("id") Integer id){
        // 根据id查询接收物资
        Goods recieveGoods = goodsService.findRecieveGoodsById(id);
        // 返回数据
        return Msg.success().add("recieveGoods",recieveGoods);
    }

    @RequestMapping(value = "/insertRecieveGoods", method = RequestMethod.POST)
    public String insertRecieveGoods(Goods goods){
        // 调用service层方法
        goodsService.insertRecieveGoods(goods);
        // 重定向到recieveGoods请求
        return "redirect: recieveGoods";
    }

    @RequestMapping(value = "/updateRecieveGoods", method = RequestMethod.POST)
    @ResponseBody
    public Msg updateRecieveGoods(Goods goods){
        // 调用service层方法
        goodsService.updateRecieveGoods(goods);
        // 返回成功信息
        return Msg.success();
    };

    @RequestMapping(value = "/deleteRecieveGoods", method = RequestMethod.POST)
    public String deleteRecieveGoods(@RequestParam("id")Integer id){
        // 调用service层方法
        goodsService.deleteRecieveGoods(id);
        // 重定向到recieveGoods请求
        return "redirect: recieveGoods";
    }

    /**
     * 搜索
     * @param key
     * @param type
     * @param model
     * @return
     */
    @RequestMapping(value = "/search")
    public String search(@RequestParam("key") String key,@RequestParam("type") String type,Model model,@RequestParam(value="pn",required = false,defaultValue = "1")Integer pn){
        //将关键字传给前端
        model.addAttribute("little_key",key);
        //开启分页查询，获取第pn页，每页5条数据
        PageHelper.startPage(pn,6);
        //根据请求搜索类型的不同，分类处理
        if(type.equals("捐赠物资") ){
            //获取搜索结果，并传给前端
            List<Goods> goodsList = goodsService.findDonateGoodsByKey(key);
            model.addAttribute("donateGoods",goodsList);
            //获取分页信息，并传给前端
            PageInfo<Goods> pageInfo = new PageInfo<>(goodsList,3);
            model.addAttribute("pageInfo", JSONObject.toJSON(pageInfo));
            return "donateGoodsListSearch";
        } else if(type.equals("接收物资")){
            //获取搜索结果，并传给前端
            List<Goods> goodsList = goodsService.findRecieveGoodsByKey(key);
            model.addAttribute("recieveGoods",goodsList);
            //获取分页信息，并传给前端
            PageInfo<Goods> pageInfo = new PageInfo<>(goodsList,3);
            model.addAttribute("pageInfo", JSONObject.toJSON(pageInfo));
            return "recieveGoodsListSearch";
        } else if(type.equals("捐赠者")){
            //获取搜索结果，并传给前端
            List<Person> personList = personService.findDonatorByKey(key);
            model.addAttribute("donators",personList);
            //获取分页信息，并传给前端
            PageInfo<Person> pageInfo = new PageInfo<>(personList,3);
            model.addAttribute("pageInfo", JSONObject.toJSON(pageInfo));
            return "donatorSearch";
        } else if(type.equals("接收者")){
            //获取搜索结果，并传给前端
            List<Person> personList = personService.findRecieverByKey(key);
            model.addAttribute("recievers",personList);
            //获取分页信息，并传给前端
            PageInfo<Person> pageInfo = new PageInfo<>(personList,3);
            model.addAttribute("pageInfo", JSONObject.toJSON(pageInfo));
            return "recieverSearch";
        }
        return null;
    }

    @ResponseBody
    @RequestMapping("/searchKeys")
    public Set<String> searchKeys(@RequestParam("key") String key,@RequestParam("type")String type){
        Set<String> keys = new HashSet<>();
        if(type.equals("捐赠物资")){
            List<Goods> donateGoods = goodsService.findDonateGoodsByKey(key);
            for(Goods goods:donateGoods){
                if(goods.getName().contains(key)){
                    keys.add(goods.getName());
                }
                if(goods.getPerson().getName().contains((key))){
                    keys.add(goods.getPerson().getName());
                }
            }
        } else if(type.equals("接收物资")){
            List<Goods> recieveGoods = goodsService.findRecieveGoodsByKey(key);
            for(Goods goods:recieveGoods){
                if(goods.getName().contains(key)){
                    keys.add(goods.getName());
                }
                if(goods.getPerson().getName().contains((key))){
                    keys.add(goods.getPerson().getName());
                }
            }
        } else if(type.equals("捐赠者")){
            List<Person> personList = personService.findDonatorByKey(key);
            for(Person person:personList){
                if(person.getName().contains(key)){
                    keys.add(person.getName());
                }
            }
        } else if(type.equals("接收者")){
            List<Person> personList = personService.findRecieverByKey(key);
            for(Person person:personList){
                if(person.getName().contains(key)){
                    keys.add(person.getName());
                }
            }
        }
        return keys;
    }
}
